package ua.org.enginer.maps
{
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.SpreadMethod;
	import flash.geom.Matrix;
	
	import mx.core.UIComponent;
	
	public class SectorMarker extends UIComponent implements IMapMarker
	{
		public var data:Object
		
		/**
		 * metrical radius
		 */
		public var radius:Number
		public var azimuth:Number
		public var color:uint
		public var angle:Number
		public var mapScale:Number = 1
		 
		public function SectorMarker(radius:Number, angle:Number, azimuth:Number=0, color:uint=0xFF0000)
		{
			super();
			
			if (radius <= 0) radius = 50
			
			this.angle = angle
			this.radius = radius
			this.color = color
			this.azimuth = azimuth
			
			
			/*
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(200, 200, 0, -100, -100);
			
			graphics.lineStyle(1, color, .9);
			graphics.beginGradientFill(GradientType.RADIAL, [0x0000FF, 0x0000FF], [.5, 0], [0, 255], matrix, 
				SpreadMethod.PAD, InterpolationMethod.LINEAR_RGB);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
			*/
		}
		
		override protected function commitProperties():void {
			drawSector(0, 0, 0, radius/mapScale, angle, azimuth)
		}
		
		private function onZChange(event:MapEvent):void {
			mapScale = _map.scale
			invalidateProperties()
		}
		
		private var _latitude:Number
		private var _longitude:Number
		
		public function get latitude():Number {
			return _latitude
		}
		public function set latitude(value:Number):void {
			_latitude = value
		}
		
		public function get longitude():Number {
			return _longitude
		}
		public function set longitude(value:Number):void {
			_longitude = value
		}		
		
		private var _map:IMap
		public function set map(value:IMap):void {
			_map = value
			_map.addEventListener(MapEvent.Z_CHANGE, onZChange)
			mapScale = _map.scale
		}
		
		private function drawSector( x:Number, y:Number, r:Number, R:Number, angle:Number, startA:Number):void
		{
			graphics.clear();
			graphics.lineStyle (1, 0, .1, true);
			
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(2*R, 2*R, 0, -R, -R);
			graphics.beginGradientFill(GradientType.RADIAL, [0x0000FF, 0x0000FF], [.5, 0], [0, 255], matrix, 
				SpreadMethod.PAD, InterpolationMethod.LINEAR_RGB);
			//graphics.beginFill(0, 0.5);
			 
			if (Math.abs(angle) > 360)
			{
				angle=360;
			}
			 
			var n:Number = Math.ceil(Math.abs(angle) / 45);
			var angleA:Number=angle / n;
			 
			angleA = angleA * Math.PI / 180;
			startA = (startA-90) * Math.PI / 180;
			 
			var startB:Number = startA;
			 
			//start edge
			graphics.moveTo(x + r * Math.cos(startA), y + r * Math.sin(startA));
			graphics.lineTo(x + R * Math.cos(startA), y + R * Math.sin(startA));
			 
			//outer arc
			for (var i:int=1; i <= n; i++)
			{
				startA += angleA;
			 
				var angleMid1:Number=startA - angleA / 2;
				var bx:Number = x + R / Math.cos(angleA / 2) * Math.cos(angleMid1);
				var by:Number = y + R / Math.cos(angleA / 2) * Math.sin(angleMid1);
				var cx:Number = x + R * Math.cos(startA);
				var cy:Number = y + R * Math.sin(startA);
				 
				graphics.curveTo(bx, by, cx, cy);
			}
			 
			// start position of inner arc
			graphics.lineTo(x + r * Math.cos(startA),y + r * Math.sin(startA));
			 
			//inner arc
			for (var j:int = n; j >= 1; j--)
			{
				startA-= angleA;
				 
				var angleMid2:Number=startA + angleA / 2;
				var bx2:Number=x + r / Math.cos(angleA / 2) * Math.cos(angleMid2);
				var by2:Number=y + r / Math.cos(angleA / 2) * Math.sin(angleMid2);
				var cx2:Number=x + r * Math.cos(startA);
				var cy2:Number=y + r * Math.sin(startA);
				 
				graphics.curveTo(bx2, by2, cx2, cy2);
			}
			 
			// end position of inner arc.
			graphics.lineTo(x + r * Math.cos(startB),y + r * Math.sin(startB));
			 
			//done
			graphics.endFill();
		}
	}
}